//
//  MCSearchBaseViewController.swift
//  StoreManage
//
//  Created by MC on 2018/9/4.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

/**
 * 通用页面  用于继承
 * 导航栏上有输入框，左侧返回按钮。右侧确定按钮
 */

typealias MCSearchBaseViewControllerClosure = (String) -> Void

class MCSearchBaseViewController: MCViewController {

    var closure : MCSearchBaseViewControllerClosure?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.addSubview(searchTextField)
    }
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        searchTextField.becomeFirstResponder()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
        searchTextField.removeFromSuperview()
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_text("确定", target: self, selector: #selector(rightItemClicked))
        
        
        searchTextField.frame = CGRect.init(x: 44, y: 6, width: MCScreenWidth - 100, height: 32)

        
        
        searchTextField.closure = { () -> Void in
            self.rightItemClicked()
        }
    }
    
    @objc func rightItemClicked() {
        
        let text = searchTextField.text ?? ""
        
        if text.isEmpty {
            MCHUD_text(message: "请输入要查询的内容", view: self.view, delay: 1.2)
            return
        }

        self.navigationController?.popViewController(animated: true)
        closure?(text)
    }
    
    
    
    lazy var searchTextField: MCTextFiled = {
        let tf = MCTextFiled()
        tf.font = MCFont(14)
        tf.textColor = UIColor.MCWords
        tf.backgroundColor = UIColor.MCGray_light
        tf.layer.cornerRadius = 16
        tf.layer.masksToBounds = true
        tf.placeholder = "请输入要查询的内容"
    
        let view = UIView()
        view.frame = CGRect.init(x: 0, y: 0, width: 10, height: 10)
        tf.leftView = view
        tf.leftViewMode = UITextFieldViewMode.always
        
        return tf
    }()
    
}
